package cn.shop.alibaba.controller;

import cn.shop.alibaba.beans.Product;
import cn.shop.alibaba.constants.HttpCode;
import cn.shop.alibaba.resp.Result;
import cn.shop.alibaba.service.ProductService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping(value = "/product/{pid}")
    public Product getProduct(@PathVariable("pid") Long pid){
       Product product =  productService.getProductByProductId(pid);
       log.info("获取到商品为：{}", JSONObject.toJSONString(product));
       return product;
    }


    @GetMapping(value = "/update_count/{pid}/{count}")
    public Result<Integer> update_count(@PathVariable("pid") Long pid,@PathVariable("count") Integer count){
       int  updateCount = productService.updateProductStockByid(count,pid);
        log.info("更新商品库存传递的参数为: 商品id:{}, 购买数量:{} ", pid, count);
        Result<Integer> result = new Result<>(HttpCode.SUCCESS,"扣除成功",updateCount);
       return  result;
    }
}
